package com.hl.card.award.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.modular.packBack.service.ISalesDivisionService;
import com.hl.card.award.service.IRewardByTrans;
import com.hl.card.award.service.ITryTestRewardByTrans;
import com.hl.card.common.persistence.dao.DisMemberInfoOenMapper;
import com.hl.card.common.remote.user.RemoteUser;
import com.hl.card.common.remote.user.RemoteUserService;
import com.hl.card.common.util.ResponseObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * java类简单作用描述
 *
 * @ProjectName: distdev
 * @Package: com.hl.card.modular.pack.service.impl
 * @ClassName: ${TYPE_NAME}
 * @Description: java类作用描述
 * @Author: 作者姓名
 * @CreateDate: 2018/7/9 17:41
 * @UpdateUser: Neil.Zhou
 * @UpdateDate: 2018/7/9 17:41
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2018</p>
 */
@Service
public class TryTestRewardByTransImpl implements ITryTestRewardByTrans {



    private static final Logger logger = LoggerFactory.getLogger(TryTestRewardByTransImpl.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    ISalesDivisionService iSalesDivisionService;

    @Autowired
    private IRewardByTrans rewardByTrans;

    public ResponseObject tryTestReward(String userId) {
        System.out.println("开始领取体验答题奖励");
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非体验用户");
        String amount = checkUser(userId);
        String cp_code = rewardByTrans.getCurrCode("CPTY");
        if (StringUtils.isNotBlank(amount)) {
            reObj= rewardByTrans.reward(userId, "CPTY", cp_code, amount);
        }
        return reObj;
    }
    public ResponseObject checkTryTestReward(String userId) {
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非体验用户");
        String amount = checkUser(userId);
        String cp_code = rewardByTrans.getCurrCode("CPTY");
        if (StringUtils.isNotBlank(amount)) {
            reObj= rewardByTrans.checkReward(userId, "CPTY", cp_code);
        }
        return reObj;
    }

    @Override
    public String checkUser(String userId) {
        JSONObject jsonObject=remoteUserService.findSsoUserByUserId(userId);
        RemoteUser remoteUser=null;
        ResponseObject reObj=new ResponseObject();
        reObj.setErroCode(1);
        reObj.setData("非体验用户");
        if (jsonObject.get("code").equals("0")) {
            JSONObject userObj = (JSONObject) jsonObject.get("user");
            remoteUser = JSON.parseObject(userObj.toJSONString(), RemoteUser.class);
            String mobile = remoteUser.getMobilePhone();
            if(StringUtils.isNotBlank(mobile)) {
                System.out.println("用户手机 = " + mobile);
                //String cp_code = stringRedisTemplate.opsForValue().get("CURR_CPTY_CODE");
                String cp_code = rewardByTrans.getCurrCode("CPTY");
                System.out.println("当前CODE = " + cp_code);
                if(StringUtils.isNotBlank(cp_code)) {
                    String mobile_str = stringRedisTemplate.opsForValue().get(cp_code);
                    System.out.println("名单 = " + mobile_str);
                    if(StringUtils.isNotBlank(mobile_str)) {
                        try {
                            JSONObject list = JSON.parseObject(mobile_str);
                            if(list != null) {
                                String temp = list.getString(mobile);
                                System.out.println("名单 = " + mobile_str);
                                System.out.println("获取的奖励信息 = " + temp);
                                if(StringUtils.isNotBlank(temp)) {
                                    return temp;
                                }
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            return null;
                        }
                    }
                }
            }
        }
        return null;
    }
}
